<?php 

error_reporting(E_ERROR);
session_start();
require_once('constants_shop.php');
require_once('functions.php');
include('classes/userClass.php');



#conect to shop database	
$shopObj = new mysqlClass();

if(isset($_POST['get_discount'])){
	$params=array($_POST['product'],$_POST['client']);
	$result  = $shopObj->fetch_one("SELECT * FROM discounts WHERE product_id=? AND client_id=?",$params);
	if(!$result['coupon_code']){
		$result['coupon_code']=generateCouponCode($shopObj);
	}

	$result['active']='no';
	if(!isset($result['active'])){
		$result['active']='no';
	}
	if($result['active']===0){
		$result['active']='yes';
	}
	$result['end']=date("m/d/Y",$result['end']);
	echo json_encode($result);
}
if(isset($_POST['send_discount'])){
	$params=array();
	$params['product_id']=$_POST['product'];
	$params['client_id']=$_POST['client'];
	$params['start'] = time();
	$params['end']= time($_POST['expiration_date']);
	$params['active']=1;
	$params['type']=$_POST['discount_type'];
	$params['coupon_code']=$_POST['coupon_code'];
	$params['discount']=$_POST['discount'];

	$data = $shopObj->fetch_one("SELECT id FROM discounts WHERE product_id='{$_POST['product']}' AND client_id='{$_POST['client']}'");
	if($data['id']>0){
		$params['id']=$data['id'];
		$shopObj->update('discounts',$params);
	}else{
		$shopObj->insert('discounts',$params);
	}
	// send mail -----------
	// update in database email status

	$params=array();
	$shopObj->execute_query("UPDATE clients_wishlists SET email_status=1 WHERE product_id='{$_POST['product']}' AND client_id='{$_POST['client']}'");
}

?>